package com.xuecheng.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.api.content.model.dto.TeachplanDTO;
import com.xuecheng.api.content.model.dto.TeachplanMediaDTO;
import com.xuecheng.api.content.model.dto.TeachplanWorkDTO;
import com.xuecheng.api.media.model.dto.MediaDTO;
import com.xuecheng.api.teaching.model.dto.WorkDTO;
import com.xuecheng.common.domain.code.CommonErrorCode;
import com.xuecheng.common.domain.response.RestResponse;
import com.xuecheng.common.enums.common.ResourceTypeEnum;
import com.xuecheng.common.enums.content.CourseModeEnum;
import com.xuecheng.common.enums.content.TeachPlanEnum;
import com.xuecheng.common.exception.ExceptionCast;
import com.xuecheng.content.agent.MediaApiAgent;
import com.xuecheng.content.agent.TeachingApiAgent;
import com.xuecheng.content.common.constant.ContentErrorCode;
import com.xuecheng.content.convert.TeachplanConvert;
import com.xuecheng.content.convert.TeachplanMediaConvert;
import com.xuecheng.content.convert.TeachplanWorkConvert;
import com.xuecheng.content.entity.CourseBase;
import com.xuecheng.content.entity.Teachplan;
import com.xuecheng.content.entity.TeachplanMedia;
import com.xuecheng.content.entity.TeachplanWork;
import com.xuecheng.content.entity.ex.TeachplanNode;
import com.xuecheng.content.mapper.TeachplanMapper;
import com.xuecheng.content.service.CourseBaseService;
import com.xuecheng.content.service.TeachplanMediaService;
import com.xuecheng.content.service.TeachplanService;
import com.xuecheng.content.service.TeachplanWorkService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.xuecheng.common.domain.code.CommonErrorCode.E_100108;

/**
 * <p>
 * 课程计划 服务实现类
 * </p>
 *
 * @author itcast
 */
@Slf4j
@Service
public class TeachplanServiceImpl extends ServiceImpl<TeachplanMapper, Teachplan> implements TeachplanService {

    @Autowired
    private CourseBaseService courseBaseService;
    @Autowired
    private TeachplanMediaService teachplanMediaService;

    @Autowired
    private TeachplanWorkService teachplanWorkService;

    @Autowired
    private MediaApiAgent mediaApiAgent;

    @Autowired
    private TeachingApiAgent teachingApiAgent;

    /**
     * <p>
     * 查询课程计划树状结构集合
     * </p>
     *
     * @return TeachplanNode
     */
    @Override
    public TeachplanDTO queryTreeNodes(Long courseId, Long companyId) {

        //判断关键数据
        verifyCourseInfo(courseId, companyId);


        List<TeachplanNode> nodes = this.getBaseMapper().selectTreeNodes(courseId);

        TeachplanDTO dtoResult;
        if (CollectionUtils.isEmpty(nodes)) {
            dtoResult = new TeachplanDTO();
        } else {
            TeachplanNode rootNode = nodes.remove(0);
            generateTreeNodes(rootNode, nodes);
            dtoResult = TeachplanConvert.INSTANCE.teachplanNodeToDTO(rootNode);
        }

        return dtoResult;
    }

    private void verifyCourseInfo(Long courseId, Long companyId) {
        //判断是否传入课程id
        if (ObjectUtils.isEmpty(courseId)) {
            ExceptionCast.cast(ContentErrorCode.E_120009);
        }
        //判断课程信息是否存在
        CourseBase courseBase = courseBaseService.getById(courseId);
        if (ObjectUtils.isEmpty(courseBase)) {
            ExceptionCast.cast(ContentErrorCode.E_120013);
        }
        //判断操作机构是否正确
        if (!ObjectUtils.isEmpty(companyId)) {

            if (!ObjectUtils.nullSafeEquals(companyId, courseBase.getCompanyId())) {
                ExceptionCast.cast(E_100108);
            }
        }
    }

    /**
     * <p>
     * 删除课程计划
     * </p>
     *
     * @param teachPlanId 课程计划id
     * @param companyId   机构id
     * @return boolean
     */
    @Override
    @Transactional
    public boolean deleteTeachPlan(Long teachPlanId, Long companyId) {

        if (ObjectUtils.isEmpty(teachPlanId)) {
            ExceptionCast.cast(ContentErrorCode.E_120401);
        }

        Teachplan teachplan = this.getById(teachPlanId);
        if (ObjectUtils.isEmpty(teachplan)) {
            ExceptionCast.cast(ContentErrorCode.E_120402);
        }
        verifyCourseInfo(teachplan.getCourseId(), companyId);

        Integer grade = teachplan.getGrade();
        if (ObjectUtils.nullSafeEquals(grade, TeachPlanEnum.FIRST_LEVEL)) {
            ExceptionCast.cast(ContentErrorCode.E_120415);
        }


        if (ObjectUtils.nullSafeEquals(grade, TeachPlanEnum.SECEND_LEVEL)) {
            //查询二级计划下是否有三级计划
            LambdaQueryWrapper<Teachplan> queryWrapper = Wrappers.lambdaQuery(Teachplan.class);
            queryWrapper
                    .eq(Teachplan::getParentid, teachplan.getId());
            int count = count(queryWrapper);
            if (count != 0) {
                ExceptionCast.cast(ContentErrorCode.E_120409);
            }

        } else {
            //查询三级计划下是否有媒资信息
            LambdaQueryWrapper<TeachplanMedia> queryWrapper = Wrappers.lambdaQuery(TeachplanMedia.class);
            queryWrapper
                    .eq(TeachplanMedia::getTeachplanId, teachplan.getId());
            int count = teachplanMediaService.count(queryWrapper);
            if (count != 0) {
                ExceptionCast.cast(ContentErrorCode.E_120414);
            }
            //查询是否绑定了作业
            LambdaQueryWrapper<TeachplanWork> workQueryWrapper = Wrappers.lambdaQuery(TeachplanWork.class)
                    .eq(TeachplanWork::getTeachplanId, teachplan.getId());
            count = teachplanWorkService.count(workQueryWrapper);
            if (count > 0) {
                ExceptionCast.cast(ContentErrorCode.E_120414);
            }
        }

        boolean flag = this.removeById(teachplan);
        if (!flag) {
            ExceptionCast.cast(ContentErrorCode.E_120407);
        }
        return true;
    }

    /**
     * <p>
     * 修改或添加课程计划
     * </p>
     *
     * @param teachplanDTO 传入的课程计划信息
     * @param companyId    机构id
     * @return TeachplanDTO
     */
    @Override
    @Transactional
    public TeachplanDTO createOrModifyTeachPlan(TeachplanDTO teachplanDTO, Long companyId) {
        if (ObjectUtils.isEmpty(teachplanDTO)) {
            ExceptionCast.cast(ContentErrorCode.E_120401);
        }

        Long courseId = teachplanDTO.getCourseId();

        this.verifyCourseInfo(courseId, companyId);

        CourseBase courseBase = courseBaseService.getById(teachplanDTO.getCourseId());
        //判断是否有课程计划id 没有说明无此计划，则创建，有则为修改
        Long teachPlanId = teachplanDTO.getTeachPlanId();
        TeachplanDTO resultDTO;
        if (ObjectUtils.isEmpty(teachPlanId)) {
            resultDTO = createTeachPlan(teachplanDTO, courseBase);
        } else {
            resultDTO = modifyTeachPlan(teachplanDTO, courseBase);
        }

        return resultDTO;
    }


    private TeachplanDTO createTeachPlan(TeachplanDTO teachplanDTO, CourseBase courseBase) {


        //获取父级节点
        Teachplan parent = getParentNode(teachplanDTO, courseBase);

        if (!ObjectUtils.nullSafeEquals(teachplanDTO.getCourseId(), parent.getCourseId())) {
            ExceptionCast.cast(ContentErrorCode.E_120406);
        }
        Teachplan teachplan = TeachplanConvert.INSTANCE.dtoToTeachPlan(teachplanDTO);


        teachplan.setParentid(parent.getId());

        teachplan.setGrade(parent.getGrade() + 1);
        //判断是否为直播课程
        String teachmode = courseBase.getTeachmode();
        if (CourseModeEnum.COURSE_MODE_LIVE_STATUS.getCode().equals(teachmode)) {
            teachplan.setMediaType(ResourceTypeEnum.LIVE.getCode());
        }


        LambdaQueryWrapper<Teachplan> queryWrapper = Wrappers.lambdaQuery(Teachplan.class)
                .eq(Teachplan::getGrade, teachplan.getGrade())
                .eq(Teachplan::getCourseId, teachplan.getCourseId());
        int count = this.count(queryWrapper);
        teachplan.setOrderby(count + 1);

        boolean save = this.save(teachplan);
        if (!save) {
            ExceptionCast.cast(ContentErrorCode.E_120407);
        }
        //System.out.println(teachplan);
        // 主键id和时间都会回填
        //Teachplan result = this.getById(teachplan.getId());
        return TeachplanConvert.INSTANCE.teachplanToDTO(teachplan);
    }

    private Teachplan getParentNode(TeachplanDTO teachplanDTO, CourseBase courseBase) {

        Long parentid = teachplanDTO.getParentid();

        //如果parentId为空 则视为2级课程计划
        if (ObjectUtils.isEmpty(parentid)) {
            //查询此课程的1级计划是否存在
            //查询条件 parentid为0
            LambdaQueryWrapper<Teachplan> wrapper = Wrappers.lambdaQuery(Teachplan.class)
                    .eq(Teachplan::getCourseId, teachplanDTO.getCourseId())
                    .eq(Teachplan::getParentid, TeachPlanEnum.FIRST_PARENTID_FLAG);
            Teachplan parent = getOne(wrapper);
            //1级计划不存在 就创建
            if (ObjectUtils.isEmpty(parent)) {
                parent = new Teachplan();
                parent.setCourseId(teachplanDTO.getCourseId());
                parent.setDescription(courseBase.getDescription());
                parent.setParentid(TeachPlanEnum.FIRST_PARENTID_FLAG.longValue());
                parent.setGrade(TeachPlanEnum.FIRST_LEVEL);
                parent.setOrderby(TeachPlanEnum.FIRST_LEVEL);
                parent.setPname(courseBase.getName());
                boolean save = this.save(parent);
                if (!save) {
                    ExceptionCast.cast(ContentErrorCode.E_120407);
                }
            }
            return parent;

        } else {
            //查询3级课程的父级课程
            LambdaQueryWrapper<Teachplan> wrapper = Wrappers.lambdaQuery(Teachplan.class)
                    .eq(Teachplan::getId, parentid);
            Teachplan parent = getOne(wrapper);
            if (ObjectUtils.isEmpty(parent)) {
                ExceptionCast.cast(ContentErrorCode.E_120408);
            }
            return parent;
        }
    }


    private TeachplanDTO modifyTeachPlan(TeachplanDTO teachplanDTO, CourseBase courseBase) {

        if (ObjectUtils.isEmpty(teachplanDTO.getParentid())) {
            ExceptionCast.cast(ContentErrorCode.E_120408);
        }
        //判断传入的planId是否存在数据
        Teachplan teachplan = getById(teachplanDTO.getTeachPlanId());
        if (ObjectUtils.isEmpty(teachplan)) {
            ExceptionCast.cast(ContentErrorCode.E_120402);
        }

        Teachplan updatedTeachplan = TeachplanConvert.INSTANCE.dtoToTeachPlan(teachplanDTO);
        //确保没有恶意数据，将parentid orderBy 等后端设置的数据和数据库的数据统一
        updatedTeachplan.setGrade(teachplan.getGrade());
        updatedTeachplan.setParentid(teachplan.getParentid());
        updatedTeachplan.setOrderby(teachplan.getOrderby());
        updatedTeachplan.setCourseId(teachplan.getCourseId());
        //判断是否为直播课程
        String teachmode = courseBase.getTeachmode();
        if (CourseModeEnum.COURSE_MODE_LIVE_STATUS.getCode().equals(teachmode)) {
            teachplan.setMediaType(ResourceTypeEnum.LIVE.getCode());
        }

        updatedTeachplan.setMediaType(null);
        boolean update = this.updateById(updatedTeachplan);
        if (!update) {
            ExceptionCast.cast(ContentErrorCode.E_120407);
        }
        return TeachplanConvert.INSTANCE.teachplanToDTO(updatedTeachplan);
    }

    private void generateTreeNodes(TeachplanNode rootNode, List<TeachplanNode> nodes) {
        //判断父节点的childrenTreeNodes是否为空
        if (CollectionUtils.isEmpty(rootNode.getChildrenTreeNodes())) {
            rootNode.setChildrenTreeNodes(new ArrayList<>());
        }
        //遍历其他节点，如果parentId是root的id 则加入root节点的childrenTreeNodes中
        for (TeachplanNode node : nodes) {
            if (ObjectUtils.nullSafeEquals(node.getParentid(), rootNode.getId())) {
                rootNode.getChildrenTreeNodes().add(node);
                //不是第三级节点节点才会迭代调用
                if (!ObjectUtils.nullSafeEquals(node.getGrade(), TeachPlanEnum.THIRD_LEVEL)) {
                    generateTreeNodes(node, nodes);
                } else {
                    //是三级节点查询绑定的媒资并赋值
                    TeachplanMedia teachplanMedia = teachplanMediaService.getOne(Wrappers.lambdaQuery(TeachplanMedia.class).
                            eq(TeachplanMedia::getTeachplanId, node.getId()));
                    //不为空则赋值
                    if (!ObjectUtils.isEmpty(teachplanMedia)) {
                        TeachplanMediaDTO dto = TeachplanMediaConvert.INSTANCE.teachplanMediaToDTO(teachplanMedia);
                        node.setTeachplanMedia(dto);
                    }
                    //查询是否有绑定了作业
                    if (ObjectUtils.isEmpty(teachplanMedia)) {
                        TeachplanWork one = teachplanWorkService.getOne(Wrappers.lambdaQuery(TeachplanWork.class)
                                .eq(TeachplanWork::getTeachplanId, node.getId()));
                        //不为空则赋值
                        if (!ObjectUtils.isEmpty(one)) {
                            TeachplanWorkDTO workDto = TeachplanWorkConvert.INSTANCE.teachplanWorkToDTO(one);
                            node.setWork(workDto);
                        }
                    }
                }
            }
        }
    }

    /**
     * <p>
     * 创建或修改课程计划绑定媒资
     * </p>
     *
     * @param teachplanMediaDTO 传入的mediaId teachplanId封装
     * @param companyId         机构id
     * @return TeachplanMediaDTO
     */
    @Override
    @Transactional
    public TeachplanMediaDTO createOrModifyTeachplanMedia(TeachplanMediaDTO teachplanMediaDTO, Long companyId) {

        Long teachplanId = teachplanMediaDTO.getTeachplanId();
        Long mediaId = teachplanMediaDTO.getMediaId();
        if (ObjectUtils.isEmpty(mediaId) || ObjectUtils.isEmpty(teachplanId)) {
            ExceptionCast.cast(CommonErrorCode.E_100116);
        }
        Teachplan teachplan = this.getById(teachplanId);
        if (ObjectUtils.isEmpty(teachplan)) {
            ExceptionCast.cast(ContentErrorCode.E_120402);
        }
        CourseBase courseBase = courseBaseService.getById(teachplan.getCourseId());
        if (ObjectUtils.isEmpty(courseBase) ||
                !ObjectUtils.nullSafeEquals(courseBase.getCompanyId(), companyId)) {
            ExceptionCast.cast(CommonErrorCode.E_100108);
        }
        // 判断是否是三级课程计划
        if (!(TeachPlanEnum.THIRD_LEVEL.equals(teachplan.getGrade()))) {
            ExceptionCast.cast(ContentErrorCode.E_120410);
        }

        //远程调用响应判断
        RestResponse<MediaDTO> response = mediaApiAgent.getMediaById(mediaId);
        if (!response.isSuccessful()) {
            ExceptionCast.cast(ContentErrorCode.E_120417);
        }
        MediaDTO mediaDTO = response.getResult();
        if (ObjectUtils.isEmpty(mediaDTO)) {
            ExceptionCast.cast(ContentErrorCode.E_120416);
        }
        //判断媒资信息是否和机构一致
        if (!ObjectUtils.nullSafeEquals(companyId, mediaDTO.getCompanyId())) {
            ExceptionCast.cast(CommonErrorCode.E_100108);
        }

        Long courseId = teachplan.getCourseId();
        //一个课程下的课程计划对应一个唯一的课程计划媒资绑定
        LambdaQueryWrapper<TeachplanMedia> queryWrapper = Wrappers.lambdaQuery(TeachplanMedia.class);
        queryWrapper.eq(TeachplanMedia::getCourseId, courseId)
                .eq(TeachplanMedia::getTeachplanId, teachplanId);
        TeachplanMedia teachplanMedia = teachplanMediaService.getOne(queryWrapper);

        boolean flag;

        if (ObjectUtils.isEmpty(teachplanMedia)) {
            //如果为空，则创建
            teachplanMedia = new TeachplanMedia();
            teachplanMedia.setTeachplanId(teachplanId);
            teachplanMedia.setMediaId(mediaId);
            teachplanMedia.setCourseId(courseId);
            teachplanMedia.setMediaFileName(mediaDTO.getFilename());
            flag = teachplanMediaService.save(teachplanMedia);
            if (!flag) {
                ExceptionCast.cast(ContentErrorCode.E_120411);
            }
        } else {
            //如果已有 修改媒资相关的信息
            teachplanMedia.setMediaId(mediaId);
            teachplanMedia.setMediaFileName(mediaDTO.getFilename());
            flag = teachplanMediaService.updateById(teachplanMedia);
            if (!flag) {
                ExceptionCast.cast(ContentErrorCode.E_120411);
            }
        }

        //绑定成功后修改课程计划的资料类型
        LambdaUpdateWrapper<Teachplan> updateWrapper = Wrappers.lambdaUpdate(Teachplan.class)
                .eq(Teachplan::getId, teachplanId)
                .set(Teachplan::getMediaType, mediaDTO.getType())
                .set(Teachplan::getChangeDate, LocalDateTime.now());
        flag = this.update(updateWrapper);
        if (!flag) {
            ExceptionCast.cast(ContentErrorCode.E_120411);
        }
        TeachplanMedia entity = teachplanMediaService.getById(teachplanMedia);

        return TeachplanMediaConvert.INSTANCE.teachplanMediaToDTO(entity);
    }


    /**
     * <p>
     * 创建或修改课程计划绑定作业
     * </p>
     *
     * @param teachplanWorkDTO 传入的作业绑定课程计划封装
     * @param companyId        机构id
     * @return TeachplanWorkDTO
     */
    @Override
    @Transactional
    public TeachplanWorkDTO createOrModifyTeachplanWork(TeachplanWorkDTO teachplanWorkDTO, Long companyId) {


        Long teachplanId = teachplanWorkDTO.getTeachplanId();
        Long workId = teachplanWorkDTO.getWorkId();
        if (ObjectUtils.isEmpty(workId) || ObjectUtils.isEmpty(teachplanId)) {
            ExceptionCast.cast(CommonErrorCode.E_100116);
        }
        //判断课程计划是否存在 三级课程
        Teachplan teachplan = this.getById(teachplanId);
        if (ObjectUtils.isEmpty(teachplan)) {
            ExceptionCast.cast(ContentErrorCode.E_120402);
        }
        //获得父级课程（二级课程）
        Long parentid = teachplan.getParentid();
        Teachplan parentPlan = this.getById(parentid);
        //判断作业是否被其他课程绑定
        LambdaQueryWrapper<TeachplanWork> queryWorkWrapper = Wrappers.lambdaQuery(TeachplanWork.class)
                .eq(TeachplanWork::getWorkId, workId);

        int count = teachplanWorkService.count(queryWorkWrapper);
        if (count > 0) {
            ExceptionCast.cast(ContentErrorCode.E_120421);
        }

        CourseBase courseBase = courseBaseService.getById(teachplan.getCourseId());
        if (ObjectUtils.isEmpty(courseBase) ||
                !ObjectUtils.nullSafeEquals(courseBase.getCompanyId(), companyId)) {
            ExceptionCast.cast(CommonErrorCode.E_100108);
        }
        // 判断是否是三级课程计划
        if (!(TeachPlanEnum.THIRD_LEVEL.equals(teachplan.getGrade()))) {
            ExceptionCast.cast(ContentErrorCode.E_120410);
        }
        //远程调用作业微服务查看作业是否存在
        RestResponse<WorkDTO> result = teachingApiAgent.getByWorkId(workId);
        if (!result.isSuccessful()) {
            ExceptionCast.cast(CommonErrorCode.E_100106);
        }

        WorkDTO workDTO = result.getResult();
        if (ObjectUtils.isEmpty(workDTO)) {
            ExceptionCast.cast(ContentErrorCode.E_120418);
        }
        Long courseId = teachplan.getCourseId();
        //一个课程计划下绑定唯一一个课程作业
        LambdaQueryWrapper<TeachplanWork> queryWrapper = Wrappers.lambdaQuery(TeachplanWork.class)
                .eq(TeachplanWork::getTeachplanId, teachplanId);

        TeachplanWork teachplanWork = teachplanWorkService.getOne(queryWrapper);

        boolean flag;
        Map<String, Object> dataMap = new HashMap<>();
        if (ObjectUtils.isEmpty(teachplanWork)) {
            //为空 则创建绑定
            teachplanWork = new TeachplanWork();
            teachplanWork.setTeachplanId(teachplanId);
            teachplanWork.setWorkId(workId);
            teachplanWork.setWorkTitle(workDTO.getTitle());
            teachplanWork.setCourseId(courseId);
            flag = teachplanWorkService.save(teachplanWork);
            if (!flag) {
                ExceptionCast.cast(ContentErrorCode.E_120419);
            }

        } else {
            //修改
            //获得原有workId
            Long formerWorkId = teachplanWork.getWorkId();
            teachplanWork.setWorkId(workId);
            teachplanWork.setWorkTitle(workDTO.getTitle());
            flag = teachplanWorkService.updateById(teachplanWork);

            if (!flag) {
                ExceptionCast.cast(ContentErrorCode.E_120419);
            }
            //修改作业信息 远程调用
            //清除原来绑定的作业的信息
            dataMap.put("courseId", null);
            dataMap.put("cname", null);
            dataMap.put("teachplanId", null);
            dataMap.put("pname", null);
            RestResponse response = teachingApiAgent.modifyWorkInfo(dataMap, formerWorkId);
            if (!response.isSuccessful()) {
                ExceptionCast.cast(CommonErrorCode.E_100106);
            }
        }
        //更新绑定之后的作业信息
        //传参 courseId cname teachplanId pname
        dataMap.put("courseId", courseId);
        dataMap.put("cname", courseBase.getName());
        dataMap.put("teachplanId", parentid);
        dataMap.put("pname", parentPlan.getPname());

        RestResponse response = teachingApiAgent.modifyWorkInfo(dataMap, workId);
        if (!response.isSuccessful()) {
            ExceptionCast.cast(CommonErrorCode.E_100106);
        }

        //绑定成功后修改课程计划的资料类型为作业
        LambdaUpdateWrapper<Teachplan> updateWrapper = Wrappers.lambdaUpdate(Teachplan.class)
                .eq(Teachplan::getId, teachplanId)
                .set(Teachplan::getMediaType, ResourceTypeEnum.WORK.getCode())
                .set(Teachplan::getChangeDate, LocalDateTime.now());
        flag = this.update(updateWrapper);
        if (!flag) {
            ExceptionCast.cast(ContentErrorCode.E_120419);
        }
        TeachplanWork entity = teachplanWorkService.getById(teachplanWork);

        return TeachplanWorkConvert.INSTANCE.teachplanWorkToDTO(entity);
    }

    /**
     * <p>
     * 删除课程计划绑定作业
     * </p>
     *
     * @param teachplanWorkId id
     * @param companyId       机构id
     */
    @Override
    @Transactional
    public void removeTeachplanWork(Long teachplanWorkId, Long courseBaseId, Long companyId) {
        if (ObjectUtils.isEmpty(teachplanWorkId)) {
            ExceptionCast.cast(ContentErrorCode.E_120420);
        }
        verifyCourseInfo(courseBaseId, companyId);
        //要解绑的记录
        TeachplanWork teachplanWork = teachplanWorkService.getById(teachplanWorkId);
        Long workId = teachplanWork.getWorkId();
        boolean remove = teachplanWorkService.removeById(teachplanWorkId);
        if (!remove) {
            ExceptionCast.cast(ContentErrorCode.E_120420);
        }
        //修改作业信息 远程调用 清除课程数据
        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("courseId", null);
        dataMap.put("cname", null);
        dataMap.put("teachplanId", null);
        dataMap.put("pname", null);
        RestResponse restResponse = teachingApiAgent.modifyWorkInfo(dataMap, workId);
        if (!restResponse.isSuccessful()) {
            ExceptionCast.cast(CommonErrorCode.E_100106);
        }
    }

    /**
     * <p>
     * 删除课程计划和媒资的绑定
     * </p>
     *
     * @param teachplanMediaId 媒资绑定id
     * @param courseBaseId     课程id
     * @param companyId        机构id
     */
    @Override
    public void removeTeachplanMedia(Long teachplanMediaId, Long courseBaseId, Long companyId) {

        if (ObjectUtils.isEmpty(teachplanMediaId)) {
            ExceptionCast.cast(ContentErrorCode.E_120420);
        }

        verifyCourseInfo(courseBaseId, companyId);

        boolean remove = teachplanMediaService.removeById(teachplanMediaId);
        if (!remove) {
            ExceptionCast.cast(ContentErrorCode.E_120420);
        }
    }
}
